package com.mercury.core.manager;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;


/**
 * @author mercury
 */
@Component
public class BloomFilterManager<T> {

    @Resource
    private RedissonClient redissonClient;


    public static final String BLOOM_FILTER = "bloom-filter";

    /**
     * 初始化值
     */
    public static final Long EXPECTED_INSERTIONS = 100000L;

    /**
     * 容错率
     */
    public static final Double FALSE_PROBABILITY = 0.03;

    private RBloomFilter<T> bloomFilter;

    @PostConstruct
    public void init() {
        RBloomFilter<T> bf = redissonClient.getBloomFilter(BLOOM_FILTER);
        bf.tryInit(EXPECTED_INSERTIONS, FALSE_PROBABILITY);
        bloomFilter = bf;
    }


    public void save(T param) {
        bloomFilter.add(param);
    }

    public Boolean contains(T param) {
        return bloomFilter.contains(param);
    }

}
